<%
   rows_by_metric_id={}
   
   # Retrieve widget settings
   metric_ids = []
   (1..10).each do |index|
     metric=widget_properties["metric#{index}"]
     if metric
       metric_ids << metric.id
       row=Sonar::TimemachineRow.new(metric)
       rows_by_metric_id[metric.id]=row
     end
   end
   if metric_ids.empty?
     # No metric has been selected, it's the first time the widget is displayed: 'ncloc' is the default metric
     ncloc = Metric.find(:first, :conditions => "name = 'ncloc'")
     metric_ids << ncloc.id
   end
   number_of_columns = widget_properties["numberOfColumns"]

   # Retrieve the measures for each metric on each snapshot
   options = {}
   from_date = dashboard_configuration.from_datetime
   if from_date
     options[:from] = from_date
   end
   snapshots=Snapshot.for_timemachine_widget(@resource, number_of_columns, options)
   sids = snapshots.collect { |s| s.id }.uniq
   measures=ProjectMeasure.find(:all,
                                :conditions =>
                                    ["snapshot_id IN (:snapshot_id) AND metric_id IN (:metric_id) AND rule_id IS NULL AND characteristic_id IS NULL AND person_id IS NULL",
                                     {:snapshot_id => sids, :metric_id => metric_ids}
                                    ]
   )

   
   # Prepare the rows to display
   snapshot_by_id={}
   snapshots.each do |s|
     snapshot_by_id[s.id]=s
   end
   measures.each do |measure|
     next unless measure.metric

     if measure.metric.timemachine? && (measure.value || measure.text_value)
       row=rows_by_metric_id[measure.metric_id]
       
       #optimization : avoid eager loading of snapshots
       measure.snapshot=snapshot_by_id[measure.snapshot_id]
       row.add_measure(measure)
     end
   end

   # Create the list of rows to display in the same order as defined by the user
   rows=[]
   metric_ids.each do |metric_id|
     row = rows_by_metric_id[metric_id]
     if row
       rows<<row
     end
   end

   # Should display the sparkline?
   sparkline_urls_by_row = {}
   if widget_properties["displaySparkLine"]
     rows.each do |row|
       sparkline_url = row.sparkline_url
       sparkline_urls_by_row[row] = sparkline_url if sparkline_url
     end
   end
   display_sparkline = !sparkline_urls_by_row.empty?
%>

<% if widget_properties["title"] %>
  <div style="position: absolute">
    <h3><%= h(widget_properties["title"]) -%></h3>
  </div>
<% end %>

<div style="overflow: auto;font-size: 12px;padding: 1px;">
  <table class="table table-bordered">

    <thead>
    <tr>
      <th></th>
      <%
         snapshots.each do |snapshot|
           event = snapshot.event('Version')
      %>
        <th nowrap="nowrap" style="vertical-align:top;text-align: right;font-size: 10px">
          <%= l snapshot.created_at.to_date -%>
          <br/>
          <%= event.name unless event==nil -%>
        </th>
      <% end %>
      <% if display_sparkline %>
        <th></th>
      <% end %>
    </tr>
    </thead>

    <tbody>
    <%
       rows.select { |row| row.metric.val_type != Metric::VALUE_TYPE_DISTRIB }.each do |row|
    %>
      <tr class="<%= cycle 'even', 'odd', :name => ('time_machine' + widget.id.to_s) -%>">
        <td width="1%" nowrap="nowrap" class="left text">
          <%= row.metric.short_name %>
        </td>
        <%
           snapshots.each do |snapshot|
             measure=row.measure(snapshot)
        %>
          <td width="1%" nowrap="nowrap" class="right"><%= format_measure(measure, :skip_span_id => true) %></td>
        <% end %>
        <%
           if display_sparkline
             sparkline_url = sparkline_urls_by_row[row]
        %>
          <td width="1%">
            <%= image_tag(sparkline_url) if sparkline_url %>
          </td>
        <% end %>
      </tr>
    <% end %>
    </tbody>

  </table>

</div>
